/**
 * 版权所有（c）2021. 上海海鼎信息工程股份有限公司，保留所有权利
 */
package com.lovely602.design.structure.flyweight;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 享元工厂角色
 *
 * @author lizhixing
 */
public class FlyweightFactory {
  private ConcurrentHashMap<String, Flyweight> flyweights = new ConcurrentHashMap<String, Flyweight>();

  public Flyweight getFlyweight(String key) {
    Flyweight flyweight = (Flyweight) flyweights.get(key);
    if (flyweight != null) {
      System.out.println("具体享元" + key + "已经存在，被成功获取！");
    } else {
      flyweight = new ConcreteFlyweight(key);
      flyweights.put(key, flyweight);
    }
    return flyweight;
  }

}
